package com.hancher.gamelife2.diary;


import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hancher.common.androidutil.AssetUtil;
import com.hancher.common.androidutil.LogUtil;
import com.hancher.common.base.BaseApplication;
import com.hancher.common.javautil.DateUtil;
import com.hancher.common.javautil.EmptyUtil;
import com.hancher.common.rv.BaseVBAdapter;
import com.hancher.common.rv.PageRecyclerViewManager;
import com.hancher.common.rv.SubRecyclerViewChildListener;
import com.hancher.gamelife2.R;
import com.hancher.gamelife2.databinding.ItemDiaryListBinding;
import com.hancher.gamelife2.weather.WeatherIconHelper;

import java.io.IOException;
import java.util.List;

public class DiaryListAdapter extends BaseVBAdapter<ItemDiaryListBinding, DiaryPO> {
    SubRecyclerViewChildListener listener;

    public DiaryListAdapter(SubRecyclerViewChildListener listener) {
        this.listener = listener;
    }

    @Override
    protected ItemDiaryListBinding createViewBinding(LayoutInflater from, ViewGroup viewGroup) {
        return ItemDiaryListBinding.inflate(from, viewGroup, false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convertItem(ItemDiaryListBinding binding, int position, DiaryPO item) {
        try {
            if (EmptyUtil.isNotEmpty(item.getWeather())) {
                String path = WeatherIconHelper.getAssetPathByWeatherStr(item.getWeather(), DateUtil.isNight(item.getCreateTime()));
                Bitmap icon = AssetUtil.getImage(BaseApplication.getInstance(), path);
                Glide.with(binding.imgWeather).load(icon).into(binding.imgWeather);
            } else {
                Glide.with(binding.imgWeather).load(R.drawable.icon_image_loss).into(binding.imgWeather);
            }
        } catch (IOException e) {
            LogUtil.e("err:", e);
        }
        String releateTime = DateUtil.getDiffDay(System.currentTimeMillis(), item.getCreateTime()) + "";
        binding.tvWeather.setText(item.getWeather() + " " + item.getTemp() + "℃ 距今" + releateTime + "天");
        binding.tvTime.setText(DateUtil.stamp2allStr(item.getCreateTime()) + " " + DateUtil.stamp2week(item.getCreateTime()));
        binding.tvContent.setText(item.getContent());
        binding.tvEquiment.setText(item.getEquipment());
        binding.tvPosition.setText(item.getAddress());
        // 图片
        List<String> images = item.getImgUrls();
        if (EmptyUtil.isEmpty(images)) {
            binding.img1.setVisibility(View.GONE);
            binding.linearLayout2.setVisibility(View.GONE);
            binding.rwImages.setVisibility(View.GONE);
            return;
        }
        if (images.size() % 3 == 1) {
            binding.img1.setVisibility(View.VISIBLE);
            updateImage(binding.img1, images.get(0));
        } else {
            binding.img1.setVisibility(View.GONE);
        }
        if (images.size() % 3 == 2) {
            binding.linearLayout2.setVisibility(View.VISIBLE);
            updateImage(binding.img2, images.get(0));
            updateImage(binding.img3, images.get(1));
        } else {
            binding.linearLayout2.setVisibility(View.GONE);
        }
        if (images.size() > 2) {
            binding.rwImages.setVisibility(View.VISIBLE);
            PageRecyclerViewManager<String> prvm = new PageRecyclerViewManager<String>()
                    .setContext(getContext())
                    .setCol(3)
                    .setRecyclerView(binding.rwImages)
                    .setAdapter(new DiaryListSubImageAdapter())
                    .setOnItemClickListener((baseQuickAdapter, view, i) -> {
                        listener.onClickSubListItemListener(position, i + images.size() % 3);
                    })
                    .init();
            List<String> rvImageStr = images.subList(images.size() % 3, images.size());
            prvm.freshData(rvImageStr);
        } else {
            binding.rwImages.setVisibility(View.GONE);
        }
    }

    public void updateImage(ImageView img1, String images) {
        if (EmptyUtil.isEmpty(images)) {
            Glide.with(img1).load(R.drawable.icon_image_loss).into(img1);
        } else {
            Glide.with(img1).load(images).into(img1);
        }
    }
}
